package com.itheima.controller;


import com.itheima.pojo.Article;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.ArticleService;
import com.itheima.utils.JwtUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {


    @Autowired
    private ArticleService articleService;

//    @GetMapping("list")
//    public Result<String> list()
//    {
//
////        //验证token
////        try {
////            Map<String,Object> claim= JwtUtil.parseToken(token);
////            return Result.success("所有文章数据");
////        } catch (Exception e) {
////            //响应401状态码
////            response.setStatus(401);
////
////            return Result.error("未登录");
////        }
////        @RequestHeader(name ="Authorization")String token, HttpServletResponse response
//
//
//        return Result.success("所有文章数据");
//
//    }


    //新增文章

    @PostMapping
    public Result add(@RequestBody @Validated Article article)
    {

        articleService.add(article);

        return Result.success();

    }


    //文章列表(条件分页)
    @GetMapping
    public Result<PageBean<Article>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
        @RequestParam(required = false) Integer categoryId,
        @RequestParam(required = false) String state
    ){


       PageBean<Article> list =articleService.list(pageNum,pageSize,categoryId,state);


       return Result.success(list);


    }


    //获取文章详情
    @GetMapping("/detail")
    public Result<Article> get(Integer id)
    {

       Article article =articleService.getById(id);


       return Result.success(article);


    }


    //更新文章
    @PutMapping
    public Result update(@RequestBody Article article)
    {

        articleService.update(article);

        return Result.success();


    }


    //删除文章
    @DeleteMapping
    public Result delete(Integer id)
    {

        articleService.delete(id);

        return Result.success();

    }






}
